package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Exam;
import cn.wolfcode.domain.Question;
import cn.wolfcode.mapper.ExamMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IExamService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {

    @Autowired
    private ExamMapper examMapper;


    @Override
    public void save(Exam exam) {
        examMapper.insert(exam);
    }

    @Override
    public void delete(Long id) {
        examMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Exam exam) {
        examMapper.updateByPrimaryKey(exam);
    }

    @Override
    public Exam get(Long id) {
        return examMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Exam> listAll() {
        return examMapper.selectAll();
    }

    @Override
    public PageInfo<Exam> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Exam> exams = examMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Exam>(exams);
    }

    @Override
    public void createExam(Exam exam) {
        //题目排序
        int count =0;
        //生成信息
        examMapper.insert(exam);
        //根据参数抽取随机题目
        //单选题
        List<Long> oneChoiceList = examMapper.selectOneChoice(exam.getOneChoiceNum());
        for (Long aLong : oneChoiceList) {
            examMapper.saveExam2Question(exam.getId(),aLong,++count,exam.getOneChoiceScore());
        }

        //多选题
        List<Long> manyChoiceList = examMapper.selectManyChoice(exam.getManyChoiceNum());
        for (Long mLong : manyChoiceList) {
            examMapper.saveExam2Question(exam.getId(),mLong,++count,exam.getManyChoiceScore());
        }
        //判断题
        List<Long> judgeList = examMapper.selectJudge(exam.getJudgeNum());
        for (Long jLong : judgeList) {
            examMapper.saveExam2Question(exam.getId(),jLong,++count,exam.getJudgeScore());
        }
        //归零让下次用
        count=0;
    }

     /*@Override
   public List<Object> selectExamPage(Long id) {
        return examMapper.selectExamPage(id);
    }*/

    @Override
    public List<Question> selectQuestionList(Long id, int type) {
        return examMapper.selectOneList(id,type);
    }

}
